package com.gift.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.gift.common.JsonMapper;
import com.gift.common.PaGing;
import com.gift.common.ServerResponse;
import com.gift.dao.GamesMapper;
import com.gift.pojo.Games;
import com.gift.service.GamesService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/3.
 */

@Controller
@RequestMapping("games")
public class GamesController {

    @Resource
    GamesService gamesService;
    @Resource
    GamesMapper gamesMapper;


    //跳转到游戏管理首页
    @RequestMapping("/home")
    public String goHome(){
        return "GamesController/GamesController";
    }

    //跳转到新增游戏页面
    @RequestMapping("/newGame")
    public String newGame(){
        return "GamesController/newGames";
    }

    //跳转到修改游戏页面
    @RequestMapping(value = "/alterGame",method = RequestMethod.GET)
    public String alterGame(@Param("gid")int gid, Model model){
        Games games=gamesMapper.selectByPrimaryKey(gid);
        model.addAttribute("games",games);
        return "GamesController/alterGame";
    }

    //得到所有经营的游戏(分页)
    @RequestMapping("/getAll")
    @ResponseBody
    public PaGing getAll(int page, int rows){

        return gamesService.getAllBypr(page,rows);
    }

    //得到所有游戏（不分页）
    @RequestMapping("/getAllNo")
    @ResponseBody
    public List getAllNo(){

        return gamesService.getAll();
    }

    //保存新增游戏
    @RequestMapping("/insetNewGame")
    @ResponseBody
    public ServerResponse insetNewGame(@Param("datalist") String datalist){

        Games newOneGames = JsonMapper.string2Obj(datalist, new TypeReference<Games>() {
        });

        return gamesService.insetNewGame(newOneGames);
    }

    //修改游戏信息
    @RequestMapping("/updataGame")
    @ResponseBody
    public ServerResponse updataGame(@Param("datalist") String datalist){

        Games alterGames = JsonMapper.string2Obj(datalist, new TypeReference<Games>() {
        });

        return gamesService.updataGame(alterGames);
    }


    //修改游戏转分比例
    @RequestMapping(value = "updataGameAcc", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updataGameAcc(@RequestParam Map<String,Object> mapO){
        Games game=gamesMapper.selectByPrimaryKey((int) mapO.get("Gid"));
        BigDecimal sellProportion=new BigDecimal(mapO.get("sellProportion").toString());
        game.setSellProportion(sellProportion);
        BigDecimal buyProportion=new BigDecimal(mapO.get("buyProportion").toString());
        game.setBuyProportion(buyProportion);
        BigDecimal stockProportion=new BigDecimal(mapO.get("stockProportion").toString());
        game.setStockProportion(stockProportion);
        return gamesService.updataGame(game);
    }



    //删除游戏信息
    @RequestMapping(value = "deleteAll", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse deleteAll(@Param("deleteIdString") String deleteIdString ) {

        String[] strings= StringUtils.split(deleteIdString,",");
        ArrayList<Integer> deleteIdList = new ArrayList<>();
        for(int i = 0;i<strings.length;i++){
            deleteIdList.add(Integer.valueOf(strings[i]));
        }
        return  gamesService.deleteGidList(deleteIdList);
    }

}
